package d1117.r214.num07;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Demo03 {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("刘备", "蜀国"));
        list.add(new Person("关羽", "蜀国"));
        list.add(new Person("张飞", "蜀国"));

        list.add(new Person("赵云", "蜀国"));
        list.add(new Person("曹操", "魏国"));
        list.add(new Person("孙权", "吴国"));
            //集合的遍历
            //增强for
            for (Person per:list){
              if(!per.getCountry().equals("蜀国"))  {
                  //修改------成功
                  //per.setCountry("蜀国");
                  //删除-------报错
                  list.remove(per);
              }

            }

            System.out.println(list);
            System.out.println("");
            //Lambda的foreach
            list.forEach(obj->{System.out.print(obj+"---");});
            //迭代器
            //创建迭代器对象
            Iterator<Person> it=list.iterator();
            while (it.hasNext()){
                Person per= it.next();
                //非蜀国人 改成蜀国人
                if(!per.getCountry().equals("蜀国")){
                    per.setCountry("蜀国");
                }
                //System.out.println(per.getName());
            }


        System.out.println(list);
        //传统for
     /*   for (int i = 0; i < list.size(); i++) {
            Person p = list.get(i);
           // System.out.println(p.getCountry());
            //是否是蜀国人
            if (!p.getCountry().equals("蜀国")) {


                list.remove(i);
            }
        }
            System.out.print(list);

      */
            //有条件的删除，修改


            // System.out.println(list);

    }
}

